﻿namespace starLiGHT.Scene
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using starLiGHT.Camera.ThreeD;
    using starLiGHT.Light;
    using starLiGHT.Render.Camera;
    using starLiGHT.Render.Camera.TwoD;
    #endregion

    public abstract class SceneManager
    {
        #region Member Variables
        private GraphicsDevice graphicsDevice;
        private AbstractCameraManager cameraManager;
        internal List<ISceneObject> sceneObjects;
        protected LightManager lightManager;

        #endregion

        public SceneManager(GraphicsDevice graphicsDevice, CameraManagerType cameraManagerType)
        {
            this.graphicsDevice = graphicsDevice;
            this.sceneObjects = new List<ISceneObject>();
            this.lightManager = new LightManager();

            // Create the camera manager
            switch (cameraManagerType)
            {
                case CameraManagerType.TwoD:
                    this.cameraManager = new CameraManager2D(graphicsDevice);
                    break;
#if !ZUNE
                case CameraManagerType.ThreeD:
                    this.cameraManager = new CameraManager3D(graphicsDevice);
                    break;
#endif
            }
        }

        public GraphicsDevice GraphicsDevice
        {
            get
            {
                return this.graphicsDevice;
            }
        }

        public LightManager LightManager
        {
            get { return this.lightManager; }
        }

        public AbstractCameraManager CameraManager
        {
            get { return this.cameraManager; }
        }

        public int SceneObjectCount
        {
            get { return this.sceneObjects.Count; }
        }

        public IEnumerable<ISceneObject> SceneObjects
        {
            get
            {
                for (int i = 0; i < this.sceneObjects.Count; i++)
                {
                    yield return this.sceneObjects[i];
                }
            }
        }

        public virtual void Update(GameTime gameTime)
        {
            if (this.cameraManager != null)
            {
                this.cameraManager.Update(gameTime);
            }
        }

        public virtual void AddSceneObject(ISceneObject obj)
        {
            this.sceneObjects.Add(obj);
        }

        public virtual void RemoveSceneObject(ISceneObject obj)
        {
            this.sceneObjects.Remove(obj);
        }

        ////#if XNA4 && !XBOX
        ////        public virtual void AddCamera(ICamera camera, string name = "Default")
        ////#else
        public virtual void AddCamera(ICamera camera)
        {
            this.AddCamera(camera, "Default");
        }

        public virtual void AddCamera(ICamera camera, string name)
        {
            this.AddCamera(camera, name, this.cameraManager.ActiveCamera == null);
        }

        public virtual void AddCamera(ICamera camera, string name, bool activate)
        ////#endif
        {
            if (camera == null)
            {
                throw new ArgumentNullException("camera");
            }

            this.cameraManager.AddCamera(name, camera, activate);
        }
    }
}
